package com.shigen.shop.controller;

import com.shigen.shop.comon.Result;
import com.shigen.shop.entity.User;
import com.shigen.shop.req.UserReq;
import com.shigen.shop.req.UserReq.RegisterReq;
import com.shigen.shop.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javax.annotation.Resource;
import javax.validation.Valid;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 用户接口
 *
 * @author shigenfu
 * @date 2022/7/25 10:12 下午
 */
@RestController
@Api(tags = "用户信息接口")
@RequestMapping(value = "user")
public class UserController {
  @Resource
  private UserService userService;

  @ApiOperation(value = "用户登录接口")
  @PostMapping(value = "login")
  public Result register(@RequestBody @Valid UserReq.LoginReq loginReq) {
    User user = userService.login(loginReq);
    return user != null ? Result.success(user): Result.err(null);
  }

  @ApiOperation(value = "用户注册接口")
  @PostMapping(value = "register")
  public Result register(@RequestBody @Valid RegisterReq register) {
    return userService.register(register) > 0 ? Result.success("注册成功"): Result.err(null);
  }
}
